package com.jrzh.mvc.convert.sys;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.jrzh.common.exception.ProjectException;
import com.jrzh.common.utils.ReflectUtils;
import com.jrzh.framework.base.convert.BaseConvertI;
import com.jrzh.mvc.model.sys.ResourcesModel;
import com.jrzh.mvc.model.sys.RoleModel;
import com.jrzh.mvc.search.sys.ResourcesSearch;
import com.jrzh.mvc.service.sys.manage.SysServiceManage;
import com.jrzh.mvc.view.sys.RoleView;

@Component("roleConvert")
public class RoleConvert implements BaseConvertI<RoleModel, RoleView>{
	
	
	@Autowired
	SysServiceManage sysServiceManage;

	public RoleModel addConvert(RoleView view) throws ProjectException {
		RoleModel model = new RoleModel();
		ReflectUtils.copySameFieldToTarget(view, model);	
		return model;
	}

	public RoleModel editConvert(RoleView view, RoleModel model) throws ProjectException {
		if(null != view.getResourceIds() && view.getResourceIds().length > 0){
			ResourcesSearch resourcesSearch = new ResourcesSearch();
			resourcesSearch.setInIds(view.getResourceIds()); 
			List<ResourcesModel> resourcesModels  =new ArrayList<ResourcesModel>();
			resourcesModels.addAll(sysServiceManage.resourcesService.list(resourcesSearch));
			model.setResources(resourcesModels);
		}
		model.setCode(view.getCode());
		model.setName(view.getName());
		model.setRemark(view.getRemark());
		model.setIsDisable(view.getIsDisable());
		model.setSortNum(view.getSortNum());
		return model;
	}

	public RoleView convertToView(RoleModel model) throws ProjectException {
		RoleView view = new RoleView();
		ReflectUtils.copySameFieldToTarget(model, view);
		return view;
	}
	
}
